package com.ycode.bang.anbang.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseFragment;
import com.ycode.bang.anbang.refresh.SwipeRefreshLayout;

import butterknife.InjectView;

/**
 * Created by squirrel on 2016/5/16.
 */
public class ExpressBaseFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    View view = null;
    @InjectView(R.id.list_event)
    RecyclerView mRecyclerView;
    // 下拉刷新，上拉加载
    @InjectView(R.id.swipe_container)
    public SwipeRefreshLayout mSwipeLayout;

    protected RecyclerView.Adapter adapter;

    protected int page=1;

    private final int refresh=11;
    private final int load=refresh+1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_public_new_handle, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
//        initData();
        initAction();
    }

    protected void initView() {
        mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.list_event);
    }

    protected void initAction() {
        initSwipe();
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(
                getActivity(), LinearLayoutManager.VERTICAL, false));
        adapter=getAdapter();
        mRecyclerView.setAdapter(adapter);
    }

    private void initSwipe() {
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setOnLoadListener(this);
        mSwipeLayout.setColor(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeLayout.setLoadNoFull(true);//是否需要上拉加载
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onLoad() {
        page=1;
        getData();
    }

    @Override
    public void onRefresh() {
        page++;
        getData();
    }

    protected RecyclerView.Adapter getAdapter() {
        return adapter;
    }
    protected void getData(){
        progressDialog= ProgressDialog.show(getActivity(),null,getActivity().getResources().getString(R.string.waiting));

    }
    protected void failedHttp(VolleyError error){
            page--;
    }
}
